package com.example.makerspace.manager;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.makerspace.dao.CourseDAO;
import com.example.makerspace.domain.CourseDO;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.utils.FileUtils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.example.makerspace.service.impl.AdminServiceImpl.managerMap;

@Component
public class CourseManager extends BaseManager<CourseDO, CourseDTO>{
    private final static String COURSE_PICTURE_PATH_PREFIX = "course/image/";
    private final static String COURSE_RESOURCE_PATH_PREFIX = "course/resource/";
    private final static List<Map<String, String>> FUZZY_QUERY_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "course_name").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "keyword").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build()
    );
  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";

   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";
    @Resource
    private CourseDAO courseDAO;

    @PostConstruct
    public void init(){
        this.baseDAO = courseDAO;
        managerMap.put("course", this);
    }

    public Result addCourse(CourseDTO courseDTO) {
        CourseDO oldCourseDO = courseDAO.selectByCourseName(courseDTO.getCourseName());
        if(oldCourseDO != null){
            return Result.error(ResultCodeEnum.COURSE_EXIST);
        }
        String imagePath = FileUtils.saveMultipartFile(courseDTO.getPicture(), FileUtils.staticPath + COURSE_PICTURE_PATH_PREFIX + courseDTO.getCourseName());
        String resourcePath = FileUtils.saveMultipartFile(courseDTO.getResource(), FileUtils.staticPath + COURSE_RESOURCE_PATH_PREFIX + courseDTO.getCourseName());
        CourseDO courseDO = new CourseDO();
        BeanUtils.copyProperties(courseDTO, courseDO);
        courseDO.setPicture(imagePath);
        courseDO.setResource(resourcePath);
        List<String> videos = Lists.newArrayList();
        courseDAO.insert(courseDO);
        return Result.success();
    }

    public Result updateCourse(CourseDTO courseDTO) {
        CourseDO courseDO = courseDAO.selectById(courseDTO.getId());
        if(courseDO == null){
            return Result.error(ResultCodeEnum.COURSE_NOT_EXIST);
        }
        Optional.ofNullable(courseDTO.getCourseName()).ifPresent(courseDO::setCourseName);
        Optional.ofNullable(courseDTO.getLevel()).ifPresent(courseDO::setLevel);
        Optional.ofNullable(courseDTO.getKeyword()).ifPresent(courseDO::setKeyword);
        Optional.ofNullable(courseDTO.getPicture()).ifPresent(picture ->{
            FileUtils.deleteFile(courseDO.getPicture());
            String imagePath = FileUtils.saveMultipartFile(picture, FileUtils.staticPath + COURSE_PICTURE_PATH_PREFIX + courseDTO.getCourseName());
            courseDO.setPicture(imagePath);
        });
        Optional.ofNullable(courseDTO.getResource()).ifPresent(resource ->{
            FileUtils.deleteFile(courseDO.getResource());
            String resourcePath = FileUtils.saveMultipartFile(resource, FileUtils.staticPath + COURSE_RESOURCE_PATH_PREFIX + courseDTO.getCourseName());
            courseDO.setResource(resourcePath);
        });
        Optional.of(courseDTO.getApplicantsNum()).ifPresent(courseDO::setApplicantsNum);
        return updateDO(courseDO);
    }

    public CourseDO selectById(int id){
        return courseDAO.selectById(id);
    }


    protected CourseDTO convert2DTO(CourseDO courseDO) {
        CourseDTO courseDTO = super.convert2DTO(courseDO);
        String picture = courseDO.getPicture();
        String resource = courseDO.getResource();
        String briefPath = FileUtils.getBriefPath(picture);
        String resourceBriefPath = FileUtils.getBriefPath(resource);
        String path =devHttpPrefix + briefPath;
        String resourceUrl = devHttpPrefix + resourceBriefPath;
        courseDTO.setResourceUrl(resourceUrl);
        courseDTO.setPicturePath(path);
        courseDTO.setResourcePath(resourceBriefPath);
        return courseDTO;
    }

    @Override
    public PageResult list(PageRequest pageRequest){
        Map<String, Object> params = pageRequest.getParams();
        Pagination pagination = pageRequest.getPagination();
        String fuzzy = MapUtils.getString(params, "fuzzy");
        if(StringUtils.isBlank(fuzzy)){
            return super.list(pageRequest);
        }
        return search(null, null, FUZZY_QUERY_PARAMS, fuzzy, pagination, CourseDO.class);
    }
}
